package com.muxi.association.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZXW
 */
public class PagingDealUtil {

    /**
     * mybatis-plus内置方法查询封装
     * @param baseMapper
     * @param page
     * @param queryWrapper
     * @return
     */
    public static Map<String, Object> pageReturnValueDeal(BaseMapper baseMapper, Page page, QueryWrapper queryWrapper) {
        IPage<Map<String, Object>> iPage = baseMapper.selectPage(page, queryWrapper);
        List<Map<String, Object>> list = iPage.getRecords();
        Map<String, Object> map = new HashMap<>(3);
        map.put("total", iPage.getTotal());
        map.put("totalPage", iPage.getPages());
        map.put("data", list);
        return map;
    }

    /**
     * 自定义查询封装
     * @param list
     * @param total
     * @param limit
     * @return
     */
    public static Map<String, Object> pageReturnValueDeal(Object list, Long total, Integer limit) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("total", total);
        map.put("totalPage", total%limit == 0 ? total/limit : total/limit+1);
        map.put("data", list);
        return map;
    }
}
